﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADTool.DotNetARX
{
    public static class BlockTools
    {
        /// <summary>
        /// 创建一个块表记录并添加到数据库中
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="blockName">块名</param>
        /// <param name="ents">加入块中的实体列表</param>
        /// <returns>返回块表记录的Id</returns>
        public static ObjectId AddBlockTableRecord(this Database db,string blockName,List<Entity>ents)
        {
            //打开块表
            BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
            if(!bt.Has(blockName) )  //判断是否存在名为blockName的块
            {
                //创建一个BlockTableRecord类的对象，表示所要创建的块
                BlockTableRecord btr=new BlockTableRecord();
                btr.Name= blockName;
                //将列表中的实体加入到新建的BlockTableRecord对象
                ents.ForEach(ent=>btr.AppendEntity(ent));
                bt.UpgradeOpen();
                bt.Add(btr);
                db.TransactionManager.AddNewlyCreatedDBObject(btr,true); //通知事务处理
                bt.DowngradeOpen();
            }
            return bt[blockName];
        }
        /// <summary>
        /// 创建一个块表记录并添加到数据库中
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="blockName">块名</param>
        /// <param name="ents">加入块中的实体列表</param>
        /// <returns>返回块表记录的Id</returns>
        public static ObjectId AddBlockTableRecord(this Database  db,string blockName,params Entity[] ents)
        {
            return AddBlockTableRecord(db,blockName,ents.ToList());
        }
        /// <summary>
        /// 为块表记录添加属性
        /// </summary>
        /// <param name="blockId">块表记录的Id</param>
        /// <param name="atts">要加入的块属性列表</param>
        public static void AddAttsToBlock(this ObjectId blockId,List<AttributeDefinition> atts)
        {
            //获取数据库对象
            Database db = blockId.Database;
            //打开块表记录为写的状态
            BlockTableRecord btr=(BlockTableRecord)blockId.GetObject(OpenMode.ForWrite);
            //遍历属性定义对象列表
            foreach(AttributeDefinition att in atts)
            {
                //为块表记录添加属性
                btr.AppendEntity(att);
                //通知事务处理
                db.TransactionManager.AddNewlyCreatedDBObject(att,true);
            }
            btr.DowngradeOpen();
        }
        /// <summary>
        /// 为块表记录添加属性
        /// </summary>
        /// <param name="blockId">块表记录的Id</param>
        /// <param name="atts">要加入的块属性列表</param>
        public static void AddAttsToBlock(this ObjectId blockId,params AttributeDefinition[] atts)
        {
            blockId.AddAttsToBlock(atts.ToList());
        }
        public static ObjectId InsertBlockReference(this ObjectId spaceId, string layer, string blockName, Point3d position, Scale3d scale, double rotateAngle)
        {
            ObjectId blockRefId;//存入要插入的块参照的Id
            Database db = spaceId.Database; //获取数据库对象
            //以读的方式打开块表
            BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
            if (!bt.Has(blockName)) return ObjectId.Null;
            BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite);
            BlockReference br = new BlockReference(position, bt[blockName]);
            br.ScaleFactors = scale;
            br.Layer = layer;
            br.Rotation = rotateAngle;
            blockRefId = space.AppendEntity(br);
            db.TransactionManager.AddNewlyCreatedDBObject(br,true);
            space.DowngradeOpen();
            return blockRefId;
        }


    }
}
